package com.yinhd.study.pachong;

import com.alibaba.fastjson.JSON;
import com.yinhd.study.entity.ProvincialData;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.System.out;

/**
 * @program: study
 * @description:
 * @Author: chengl
 * @create: 2022-04-01 14:02
 **/
@Service
public class JsoupTest {
    public void getData() throws IOException {

        Document document = Jsoup.connect("https://ncov.dxy.cn/ncovh5/view/pneumonia")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0")
                .header("Connection", "close")//如果是这种方式，这里务必带上
                .timeout(8000)//超时时间
                .get();

        out.println(document.html());
        String text = document.select("script[id=fetchRecentStatV2]").toString();
        String pattren = "\\[(.*)\\]";
        Pattern compile = Pattern.compile(pattren);
        Matcher matcher = compile.matcher(text);
        String jsonStr = "";
        if (matcher.find()){
            jsonStr = matcher.group(0);
            Object parse = JSON.parse(jsonStr);
            out.println((JSON.toJSONString(parse,true)));
        }else{
            out.println("没有获取到初始数据");
        }
        List<ProvincialData> provincialData = JSON.parseArray(jsonStr, ProvincialData.class);
        for (ProvincialData data : provincialData) {
            Document document1 = Jsoup.connect(data.getStatisticsData())
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0")
                    .header("Connection", "close")//如果是这种方式，这里务必带上
                    .ignoreContentType(true)
                    .timeout(8000).get();
            Element body = document1.body();
            String text1 = body.text();
            Object parse2 = JSON.parse(text1);
            out.println((JSON.toJSONString(parse2, true)));
        }
    }

}
